package action.ManagerAction;

import com.opensymphony.xwork2.ActionSupport;
import entity.CroUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import service.CroUserService;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import java.util.UUID;

/**
 * Created by xuweijie on 17-5-13.
 */
public class PersonalCenterAction extends ActionSupport {

    @Autowired
    private CroUserService croUserService;

    private CroUser croUser;
    private String successResult;
    private String oldpassword;
    private String newpassword;
    private String checkpassword;
    private String cusername;

    /*
    * 个人信息
    * */
    @RequiresPermissions("user:select")
    public String PersonalDetail(){
        ServletContext context = ServletActionContext.getServletContext();
        String username= (String) context.getAttribute("username");
        croUser=croUserService.findUserByName(username);
        return SUCCESS;
    }

    /*
    * 修改密码
    * */
    @RequiresPermissions("user:update")
    public String ModifyPwd(){
        croUser=croUserService.findUserByName(cusername);
        if((new Md5Hash(oldpassword,croUser.getSlat()).toString()).equals(croUser.getPassword())){
            if(newpassword.equals(checkpassword)){
                String slat=UUID.randomUUID().toString().substring(0,8);
                newpassword=new Md5Hash(newpassword,slat).toString();
                croUser.setSlat(slat);
                croUser.setPassword(newpassword);
                if(croUserService.updateUser(croUser)){
                    successResult="2";//修改成功
                }else {
                    successResult="3";//修改失败
                }
            }else{
                successResult="1";//两次密码输入不同
            }
        }else {
            successResult="0";//旧密码验证不通过
        }
        return SUCCESS;
    }

    public CroUser getCroUser() {
        return croUser;
    }

    public void setCroUser(CroUser croUser) {
        this.croUser = croUser;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String getOldpassword() {
        return oldpassword;
    }

    public void setOldpassword(String oldpassword) {
        this.oldpassword = oldpassword;
    }

    public String getNewpassword() {
        return newpassword;
    }

    public void setNewpassword(String newpassword) {
        this.newpassword = newpassword;
    }

    public String getCheckpassword() {
        return checkpassword;
    }

    public void setCheckpassword(String checkpassword) {
        this.checkpassword = checkpassword;
    }

    public String getCusername() {
        return cusername;
    }

    public void setCusername(String cusername) {
        this.cusername = cusername;
    }
}
